<template>
  <div class="manager-container">
    <!-- 搜索条件开始-->
    <el-row :gutter="20">
      <el-col :span="20">
        <el-form :model="queryParams" ref="queryForm" :inline="true">
          <el-col :span="12">
            <el-form-item label="字典名称" prop="name">
              <el-input
                v-model="queryParams.name"
                placeholder="请输入字典名称"
                clearable
                size="default"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分类名称" prop="sortName">
              <el-input
                      v-model="queryParams.sortName"
                      placeholder="请输入分类名称"
                      clearable
                      size="default"
                      @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分类类型" prop="sortType">
              <el-input
                      v-model="queryParams.sortType"
                      placeholder="请输入分类类型"
                      clearable
                      size="default"
                      @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分类编码" prop="sortCode">
              <el-input
                      v-model="queryParams.sortCode"
                      placeholder="请输入分类编码"
                      clearable
                      size="default"
                      @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>

          <el-col :span="1.5">
            <el-button type="primary" size="default" @click="handleQuery">
              <i class="iconfont icon-chaxun">查询</i>
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button size="default" @click="resetQuery">
              <i class="iconfont icon-chongzhi">重置</i>
            </el-button>
          </el-col>
        </el-form>
      </el-col>
      <el-col :span="4">
        <!-- 工具条开始 -->
        <el-row :gutter="20" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" size="default" @click="handleAdd">
              <i class="iconfont icon-tianjia">新增</i>
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button style="width: 110px" type="info" plain size="default" @click="toggleExpandAll">
              <i class="iconfont icon-paixu">展开/折叠</i>
            </el-button>
          </el-col>
        </el-row>
      </el-col>
    </el-row>
    <!-- 搜索条件结束 -->
    <!-- 工具条结束 -->

    <!-- 主页面开始 -->
    <el-table
      :header-cell-style="{background:'#fafafa'}"
      :border="true"
      v-loading="loading"
      :data="deptList"
      row-key="id"
      :default-expand-all="isExpandAll"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      height="600"
    >
      <el-table-column prop="name" label="字典名称" align="center">
        <template #default="scope">
          <span>{{scope.row.name || '-'}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="code" label="字典编码" align="center">
        <template #default="scope">
          <span>{{scope.row.code || '-'}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="sortName" label="分类名称" align="center">
        <template #default="scope">
          <span>{{scope.row.sortName || '-'}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="sortType" label="分类类型" align="center">
        <template #default="scope">
          <span>{{scope.row.sortType || '-'}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="sortCode" label="分类编码" align="center">
        <template #default="scope">
          <span>{{scope.row.sortCode || '-'}}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template #default="scope">
          <el-button size="default" type="text" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-divider direction="vertical"></el-divider>
          <el-button plain size="default" type="text" @click="handleDelete(scope.row)">删除</el-button>
          <el-divider direction="vertical"></el-divider>
          <el-button plain size="default" type="text" @click="handleAddDown(scope.row)">新增下级</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 主页面结束 -->
    <!-- 添加或修改部门对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24" v-if="false">
            <el-form-item label="字典父id" prop="pid">
              <el-input v-model="form.pid" placeholder="请输入字典名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="分类名称" prop="sortName">
              <el-input v-model="form.sortName" placeholder="请输入分类名称" :disabled="form.id !=undefined"/>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="24">
             <el-form-item label="分类编码" prop="sortCode">
               <el-input v-model="form.sortCode" placeholder="请输入分类编码" :disabled="form.id !=undefined"/>
             </el-form-item>
           </el-col>-->
          <el-col :span="24">
            <el-form-item label="分类类型" prop="sortType">
              <el-input v-model="form.sortType" placeholder="请输入分类类型" :disabled="form.id !=undefined"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="字典名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入字典名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="字典编码" prop="code">
              <el-input v-model="form.code" placeholder="请输入字典编码"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
      </template>
    </el-dialog>
    <el-dialog :title="title" v-model="flagShow" width="600px" append-to-body>
      <el-form ref="form" :model="formList" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="父级节点" prop="pName">
                <el-input v-model="pName" placeholder="请输入父级节点" disabled/>
              <!-- <tree-select
                :value="formList.pid"
                v-model="formList.pid"
                :options="deptOptions"
                :show-count="true"
                :normalizer='normalizer'
                placeholder="选择父级节点"
                :objMap="{
                    value: 'id',
                    label: 'name',
                    children: 'children',
                }"
              /> -->
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="字典名称" prop="name">
              <el-input v-model="formList.name" placeholder="请输入字典名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="字典编码" prop="code">
              <el-input v-model="formList.code" placeholder="请输入字典编码"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="submitDown">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
//引入消息弹出框
import { ElMessageBox, ElMessage } from "element-plus";
import api from "@/api";
import { formTree } from "@/utils/index";
//引入公共组件
import TreeSelect from "@/components/TreeSelect/index.vue"; //下拉树
export default {
  name: "Dept",
  components: { TreeSelect },
  data() {
    return {
      pName:'',
      // 遮罩层
      loading: true,
      // 表格树数据
      deptList: [],
      // 部门树选项
      deptOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      flagShow: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        //查询参数
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {
      },
      formList:{},
      // 表单校验
      rules: {
        code: [
          { required: false, message: "字典编码不能为空", trigger: "blur" }
        ],
        name: [
          { required: false, message: "字典名称不能为空", trigger: "blur" }
        ],
       /* sortName: [
          { required: true, message: "分类名称不能为空", trigger: "change" }
        ],*/
        sortType: [
          { required: true, message: "分类编码不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询部门列表 */
    getList() {
      this.loading = true;
      api.classify
        .list(this.queryParams)
        .then(response => {
          this.deptList = formTree(response.rows, "id");
          this.loading = false;
        })
        .catch(err => {
          this.$message.error(err.msg);
        });
    },
    /** 转换数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      };
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.flagShow = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        sortCode: undefined,
        sortName: undefined,
        name: undefined,
        code: undefined,
        pid: undefined,
      };
      this.formList = {
        name: undefined,
        code: undefined,
        pid: undefined
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs.queryForm.resetFields();
      this.handleQuery();
    },
    /** 新增按钮操作 */
    async handleAdd(row) {
      this.reset();
      this.form.pid = '0';
      this.open = true;
      this.title = "添加分类字典";
    },
    /** 新增下级按钮操作 */
    async handleAddDown(row) {
      this.reset();
      // await this.getTreeselect();
      this.pName = row.name;
      this.formList.pid = row.id;
      this.formList.sortCode = row.sortCode
      this.formList.sortName = row.sortName
      this.formList.sortType = row.sortType
      this.flagShow = true;
      this.title = "添加分类字典下级";
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      api.classify.list(this.queryParams).then(response => {
        this.deptOptions = [];
        const dept = {
          id: '0',
          name: "分类字典",
          children: []
        };
        dept.children = formTree(response.rows, "id");
        this.deptOptions.push(dept);
        // this.deptOptions = handleTree(response.data, "deptId");
      });
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    async handleUpdate(row) {
      this.reset();
      // await this.getTreeselect();
      // if (row != null && row.deptId) {
      //   this.form.pid = row.pid;
      // } else {
      //   this.form.pid = 0;
      // }
      api.classify.editId(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改分类字典";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          console.log(this.form.pid,"123");

          if (this.form.id != undefined) {
            api.classify.edit(this.form).then(response => {
              this.$message.success(response.msg);
              this.open = false;
              this.getList();
            }).catch(err=>{
            this.$message.error(err.msg);
          });
          } else {
            api.classify.add(this.form).then(response => {
              this.$message.success(response.msg);
              this.open = false;
              this.getList();
            }).catch(err=>{
            this.$message.error(err.msg);
          });
          }
        }
      });
    },
    /** 提交按钮 */
    submitDown: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          api.classify.add(this.formList).then(response => {
            this.$message.success(response.msg);
            this.flagShow = false;
            this.getList();
          }).catch(err=>{
            this.$message.error(err.msg);
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm(
        '是否确认删除名称为"' + row.name + '"的数据项？',
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return api.classify.del(row.id);
        })
        .then(() => {
          this.getList();
          //弹出提示
          this.$message.success("删除成功");
        })
        .catch(() => {
          this.$message.info("已取消");
        });
    }
  }
};
</script>
<style lang="less" scoped>
</style>
